<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <h3>{{infoName}}  &  视频</h3>
      <!--<div v-if="crud.props.searchToggle">
        &lt;!&ndash; 搜索 &ndash;&gt;
        <el-input v-model="query.value" clearable placeholder="输入搜索内容" style="width: 200px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-select v-model="query.type" clearable placeholder="类型" class="filter-item" style="width: 130px">
          <el-option v-for="item in queryTypeOptions" :key="item.key" :label="item.display_name" :value="item.key" />
        </el-select>
        <el-date-picker
          v-model="query.createTime"
          :default-time="['00:00:00','23:59:59']"
          type="daterange"
          range-separator=":"
          size="small"
          class="date-item"
          value-format="yyyy-MM-dd HH:mm:ss"
          start-placeholder="createTimeStart"
          end-placeholder="createTimeEnd"
        />
        <rrOperation :crud="crud" />
      </div>-->
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" >
        <el-button
          slot="right"
          v-permission="['admin','album:back']"

          class="filter-item"
          size="mini"
          type="primary"
          @click="back"
        >返回上一级</el-button>
      </crudOperation>
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="视频名称">
            <el-input v-model="form.name" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="缩略图">
            <el-upload
              v-model="form.defaultPhoto"
              class="avatar-uploader"
              action="http://www.xinhongdou.cn:8088/upload/img"
              name="srcFile"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="form.defaultPhoto" :src="form.defaultPhoto" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon" />
            </el-upload>
          </el-form-item>
          <el-form-item label="视频文件">
            <el-upload
              v-model="form.path"
              class="upload-demo"
              action="http://www.xinhongdou.cn:8088/upload/img"
              name="srcFile"
              :limit="1"
              :on-exceed="handleExceed"
              :on-success="handleSuccess"
              :before-upload="beforeUpload"
            >
              <el-button size="small" type="primary">点击上传</el-button>

            </el-upload>
          </el-form-item>
          <el-form-item label="创建时间">
            <el-date-picker v-model="form.createTime" type="datetime" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column v-if="columns.visible('name')" prop="name" label="视频名称" />
        <el-table-column v-if="columns.visible('defaultPhoto')" prop="path" label="缩略图" >
          <template slot-scope="scope"><img style="height: 80px" :src="scope.row.defaultPhoto"></template>
        </el-table-column>
        <el-table-column v-if="columns.visible('path')" prop="path" label="视频路径" />
        <el-table-column v-if="columns.visible('createTime')" prop="createTime" label="创建时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-permission="['admin','video:edit','video:del']" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudVideo from '@/api/video'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

// crud交由presenter持有
const defaultCrud = CRUD({ title: '视频管理', url: 'api/video', sort: 'id,desc', crudMethod: { ...crudVideo }})
const defaultForm = { id: null, name: null, defaultPhoto: null, path: null, createTime: null, infoId: null, type: null }
export default {
  name: 'Video',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  data() {
    return {
      infoName:'',
      permission: {
        add: ['admin', 'video:add'],
        edit: ['admin', 'video:edit'],
        del: ['admin', 'video:del']
      },
      rules: {
      },
      queryTypeOptions: [
        { key: 'infoId', display_name: '根据type填写相对应id' },
        { key: 'type', display_name: '类型' }
      ]
    }
  },
  created(){
    this.init();
  },
  methods: {
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      const query = this.query
      this.crud.params['infoId'] = this.$route.query.id;
      this.crud.params['type'] =this.$route.query.type;
      return true
    },

    [CRUD.HOOK.beforeToAdd]() {
      //const query = this.query
      this.form.infoId= this.$route.query.id;
      this.form.type= this.$route.query.type;
      return true
    },

    init(){
      this.form.infoId= this.$route.query.id;
      this.form.type= this.$route.query.type;
      this.infoName=this.$route.query.name;

    },


    handleSuccess(res, file) {
      this.form.path = URL.createObjectURL(file.raw)
      this.form.path = res.message
      console.log('成功上传，url=' + res.message)
    },
    handleExceed(files, fileList){
      this.$notify({
        title: '警告',
        message: '只允许上传单个文件',
        type: 'warning'
      });
    },


    beforeUpload(file) {

     /* const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M*/
    },

    handleAvatarSuccess(res, file) {
      this.form.defaultPhoto = URL.createObjectURL(file.raw)
      this.form.defaultPhoto = res.message
      console.log('成功上传，url=' + res.message)
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },

    back(){
      this.$router.back(-1);
    }




  }
}
</script>

<style scoped>
  avatar-uploader .el-upload {
    border: 1px dashed #324157;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    border: 1px solid #b4bccc;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
